Get premium membership and access questions with answers, video lessons as well as revision papers.

Given the following class, class summation{ int num; long sum; //summation of num public: void set_sum(int n); void show_sum () { cout <

      

Given the following class,
class summation{
int num;
long sum; //summation of num
public:
void set_sum(int n);
void show_sum () {
cout <}
};
void summation::set _sum(int n)
{
int i;
num = n;
sum = 0;
for(i=1; i<=n; i++)
sum += i;
}
a) create a C++ function called make_sum() that returns an object of type summation. Have this function prompt the user for a number and then construct an object having this value and return it to the calling procedure.Demonstrate that the function works.
b)The function set_sum() was not defined in line within the summation class declaration. Give a reason why this might be necessary for some compilers.

  

Answers


Davis
a)#include
using namespace std;
class summation {
int num;
long sm; //summation of num
public:
void set_sum(int n);
void show_sum () {
cout <}
};
void summation::set_sum(int{
int i;
num = n;
sum = 0;
for(i=1; i<=n; i++)
sum += i;
}
summation make_ ()
{
int i;
summation temp;
cout << "Enter number: ";
cin >> i;
temp.ssum(i);
return temp; }
int main()
{
summation s;
s= make_sum();
s.show_sum();
return 0;
}

b)For some compilers, in-line functions cannot containsops.
Githiari answered the question on May 31, 2018 at 17:45


Next: What is wrong with the following prototype, which uses a default argument?int f(int count, int max = count);
Previous: Other than competition, explain the impact of external factors on an organisation

View More Computer Studies Questions and Answers | Return to Questions Index


Learn High School English on YouTube

Related Questions